﻿using Duende.IdentityServer.Services;
using MediatR;
using Service.Auth.Api.Application.Notification;
using Service.Auth.Domain.AggregatesModel;
using Service.Core.Redis.Services;

namespace Service.Auth.Api.Application.NotificationHandlers
{
    /// <summary>
    /// 注销登陆通知处理器
    /// </summary>
    public class LogoutAccountHandlers(IRedisService redisService, IPersistedGrantService persistedGrantService) : INotificationHandler<LogoutAccountNotification>
    {
        public async Task Handle(LogoutAccountNotification notification, CancellationToken cancellationToken)
        {
            await redisService.Database.KeyDeleteAsync($"User:{notification.UserId}");
            await redisService.Database.KeyDeleteAsync($"User:{notification.UserId}:latestJti");
            await persistedGrantService.RemoveAllGrantsAsync(notification.UserId.ToString());

        }
    }
}
